/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.brainstretch.recommend;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.*;
import org.brainstretch.models.quizzes.Category;

/**
 *
 * @author mrodaitis
 */
@Entity
public class Profile implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    
    @OneToMany(cascade= CascadeType.ALL)
    private List<CategoryWeight> categoryWeights;
    
    
    private double averageAttemptsPerQuestion;
    private double averageAnswerLength;
    private double averageQuestionLength;
    private double averageScore;
    private double questionsPerSet;
    private double timePerQuestion;
    private double duration;
    
    @Temporal(TemporalType.TIMESTAMP)
    private Date lastUpdated;

    public Profile() {
        categoryWeights = new ArrayList<CategoryWeight>();
        averageAnswerLength = 0;
        averageAttemptsPerQuestion= 0;
        averageQuestionLength = 0;
        averageScore = 0;
        timePerQuestion = 0;
        questionsPerSet = 0;
        for(Category cat: Category.values()){
            categoryWeights.add(new CategoryWeight(cat, 0));
        }
        lastUpdated = new Date();
    }
    
        
    public double getCategoryWeight(Category category){
        double weight = 0;
        boolean found = false;
        for(int i = 0; i< categoryWeights.size() && !found; i++){
            CategoryWeight catW = categoryWeights.get(i);
            found = catW.getCategory() == category;
            if(found){
                weight = catW.getWeight();
            }
        }
        return weight;
    }
    
    public void setCategoryWeight(Category category, double weight){
        boolean found = false;
        for(int i = 0; i< categoryWeights.size() && !found; i++){
            CategoryWeight catW = categoryWeights.get(i);
            found = catW.getCategory() == category;
            if(found){
                catW.setWeight(weight);
            }
        }
    }


    public double getAverageAnswerLength() {
        return averageAnswerLength;
    }

    public void setAverageAnswerLength(double averageAnswerLength) {
        this.averageAnswerLength = averageAnswerLength;
    }

    public double getAverageAttemptsPerQuestion() {
        return averageAttemptsPerQuestion;
    }

    public void setAverageAttemptsPerQuestion(double averageAttemptsPerQuestion) {
        this.averageAttemptsPerQuestion = averageAttemptsPerQuestion;
    }

    public double getAverageQuestionLength() {
        return averageQuestionLength;
    }

    public void setAverageQuestionLength(double averageQuestionLength) {
        this.averageQuestionLength = averageQuestionLength;
    }

    public double getAverageScore() {
        return averageScore;
    }

    public void setAverageScore(double averageScore) {
        this.averageScore = averageScore;
    }

    public List<CategoryWeight> getCategoryWeights() {
        return categoryWeights;
    }

    public void setCategoryWeights(List<CategoryWeight> categoryWeights) {
        this.categoryWeights = categoryWeights;
    }

    public Date getLastUpdated() {
        return lastUpdated;
    }

    public void setLastUpdated(Date lastUpdated) {
        this.lastUpdated = lastUpdated;
    }

    public double getQuestionsPerSet() {
        return questionsPerSet;
    }

    public void setQuestionsPerSet(double questionsPerSet) {
        this.questionsPerSet = questionsPerSet;
    }

    public double getTimePerQuestion() {
        return timePerQuestion;
    }

    public void setTimePerQuestion(double timePerQuestion) {
        this.timePerQuestion = timePerQuestion;
    }

    public double getDuration() {
        return duration;
    }

    public void setDuration(double duration) {
        this.duration = duration;
    }
    
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
    

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Profile)) {
            return false;
        }
        Profile other = (Profile) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }
    
    protected void setValuesTo(ProfileVector vector){
        for(ProfileDimension dim: ProfileDimension.values()){
            double value = vector.getPropertyWeight(dim);
            switch(dim){
                case AnswerLength: averageAnswerLength = value; break;
                case AttmeptsPerQuestion: averageAttemptsPerQuestion = value; break;
                case AvgScore: averageScore = value; break;
                case Duration: duration = value; break;
                case QuestionLength: averageQuestionLength = value; break;
                case QuestionsPerSet: questionsPerSet = value; break;
                case TimePerQuestion: timePerQuestion = value; break;
            }
        }
        for(Category cat: Category.values()){
            setCategoryWeight(cat, vector.getCategoryWeight(cat));
        }
    }

    @Override
    public String toString() {
        return "org.brainstretch.recommend.Profile[ id=" + id + " ]";
    }
    
}
